﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JasRibbonApplicatie.Classes
{
    public class EmailAdres
    {

        public string Naam { get; set; }
        public string Host { get; set; }
        public string Mailbox { get; set; }
        public string Email
        {
            get
            {
                return Mailbox + "@" + Host;
            }
            set
            {
                var mail = value.Split('@');
                if (mail.Count() == 2)
                {
                    Host = mail[1];
                    Mailbox = mail[0];
                }
                else
                {
                    throw new FormatException("This is not a valid Emailaddress");
                }
            }
        }

        public EmailAdres(string naam, string email)
        {
            Naam = naam;
            Email = email;
        }
        public EmailAdres(string jsonString)
        {
            //Ex: [{"personal":"Johan Vandeplas","mailbox":"johanvandeplas","host":"hotmail.com"}]
            //strip haakjes
            jsonString = jsonString.Substring(2, jsonString.Length - 4);
            //split bij ,
            string[] jsonparseArray = jsonString.Split(',');
            //loop over de vars
            foreach (var v in jsonparseArray)
            {
                string[] varray = v.Split(':');
                //set right var and strip dblquotes
                switch (varray[0].Substring(1, varray[0].Length - 2))
                {
                    case "personal":
                        Naam = varray[1].Substring(1, varray[1].Length - 2);
                        break;
                    case "mailbox":
                        Mailbox = varray[1].Substring(1, varray[1].Length - 2);
                        break;
                    case "host":
                        Host = varray[1].Substring(1, varray[1].Length - 2);
                        break;
                }
            }
        }
        public string ToJson()
        {
            return "[{\"personal\":\"" + Naam + "\",\"mailbox\":\"" + Mailbox + "\",\"host\":\"" + Host + "\"}]";
        }
        public override string ToString()
        {
            if (Naam != "")
                return Naam;
            else
                return Email;
        }
    }
}
